

public class MyRobot
{
	public static final int FRONT = 0, BACK = 1, LEFT = 2, RIGHT = 3;
	public static DummyMove rover;
	//public static Move rover;
	public static MyMaze myMaze;
	
	public static void turn90Ntimes(int direction,int n)
	{
		for(int i=0;i<n;i++)
		{
			if(direction == MyMaze.GOLEFT)
			//if(direction == rover.GOLEFT)	
			{
			   //rover.chgDir(rover.GOLEFT);
			   myMaze.chgDir(MyMaze.GOLEFT);
			   rover.left();
			}
			else
			{
			   //rover.chgDir(rover.GORIGHT);
			   myMaze.chgDir(MyMaze.GORIGHT);
			   rover.right();
			}
		}
	}	
		
	public static void main(String arg[])
	throws InterruptedException
	{
		// Move rover = new Move();
		rover = new DummyMove();
		myMaze = new MyMaze();

		/*
		 * state is the current intent of the robot
		 */

		int state = FRONT;
		boolean backtrack = false;
		//myMaze.ConfirmMaze(0);
		
		while (true)
		{
			//if( rover.checkgoal() )
			if (myMaze.checkgoal())
			{
				rover.lcd("GOAL");
				break;
			}
			
			//if((state = myMaze.findUnexploredNeighbor()) != -1)
			//if(backtrack==true || ((state = rover.findUnexploredNeighbor()) != -1))
			if (backtrack == true || ((state = myMaze.findUnexploredNeighbor()) != -1))
			{
				backtrack = false;
				
				if(state == MyRobot.LEFT)
				{
					turn90Ntimes(MyMaze.GOLEFT,1);
					//turn90Ntimes(rover.GOLEFT,1);
				
					if (rover.forward(1))
					{
						myMaze.chgPos(MyMaze.FORWARD, 1);
						rover.print(myMaze.position[0],myMaze.position[1]);
						
						//rover.chgPos(rover.FORWARD, 1);
						//rover.print(rover.position[0],rover.position[1]);
					}
					else 
					{
						myMaze.putWall();
						turn90Ntimes(MyMaze.GORIGHT,1);
						
						//rover.putWall();
						//turn90Ntimes(rover.GORIGHT,1);
					}
				}
				else if(state == MyRobot.FRONT)
				{
					if (rover.forward(1))
					{
						myMaze.chgPos(MyMaze.FORWARD, 1);
						rover.print(myMaze.position[0],myMaze.position[1]);
						
						//rover.chgPos(rover.FORWARD, 1);
						//rover.print(rover.position[0],rover.position[1]);
					}
					else
					{
						//rover.putWall();
						myMaze.putWall();
					} 
						
				}
				else if(state == MyRobot.RIGHT)
				{
					turn90Ntimes(MyMaze.GORIGHT,1);
					//turn90Ntimes(rover.GORIGHT,1);
					
					if (rover.forward(1))
					{
						myMaze.chgPos(MyMaze.FORWARD, 1);
						rover.print(myMaze.position[0],myMaze.position[1]);
						
						//rover.chgPos(rover.FORWARD, 1);
						//rover.print(rover.position[0],rover.position[1]);	
					}
					else 
					{
						myMaze.putWall();
						turn90Ntimes(MyMaze.GOLEFT,1);
						
						//rover.putWall();
						//turn90Ntimes(rover.GOLEFT,1);
					}
				}
				else if(state == MyRobot.BACK)
				{
					//turn90Ntimes(MyMaze.GOLEFT,2);
					turn90Ntimes(rover.GOLEFT,2);
					
					if (rover.forward(1))
					{
						myMaze.chgPos(MyMaze.FORWARD, 1);
						rover.print(myMaze.position[0],myMaze.position[1]);	
						//rover.chgPos(rover.FORWARD, 1);
						//rover.print(rover.position[0],rover.position[1]);	
					}
					else 
					{
						myMaze.putWall();
						turn90Ntimes(MyMaze.GORIGHT,2);
						
						//rover.putWall();
						//turn90Ntimes(rover.GORIGHT,2);
					}
				}
			}
			else if (!myMaze.checkstart())
			//else if (!rover.checkstart())
			{
				System.out.println("In backtrack");
				
				//if((state = rover.backtrack())!=-9999)
				//	backtrack=true;
				
				if((state = myMaze.backtrack())!=-9999)
					backtrack=true;	
				
				//TODO: need to double check backtracking
			}
			else
			{
				rover.lcd("GOAL cannot be reached");
				break;
			}
		
			/*
			if (state == FRONT)
			{
				if (/*!myMaze.getWall(FRONT) 
						&& rover.forward(1))
				{
					//myMaze.chgPos(MyMaze.FORWARD, 1);
					rover.print(myMaze.position[0],myMaze.position[1]);
					state = LEFT;
				}
				else {
					//myMaze.putWall();
					state = RIGHT;
				}
			}
			else 
			{
				if (state == BACK) //unreachable state at the moment
				{
					rover.backward(1);
					//myMaze.chgPos(MyMaze.BACKWARD, 1);
					state = RIGHT;
				}
				else
				{
					if (state == LEFT)
					{
						//myMaze.chgDir(MyMaze.GOLEFT);
						rover.left();
						state = FRONT;
					}
					else // State == RIGHT
					{
						//myMaze.chgDir(MyMaze.GORIGHT);
						rover.right();
						state = FRONT;
					}
				}
			}*/
		}
	}
}
